package com.sanhuo.lib;



import com.sanhuo.lib.exception.GlobalException;

import java.lang.reflect.InvocationTargetException;

public class ExceptionUtil {

    public static void throwException(boolean condition, Integer code, String msg, Class<? extends GlobalException> exceptionClass) {
        try {
            if (condition) {
                throw exceptionClass.getConstructor(Integer.class, String.class)
                        .newInstance(code, msg);

            }
        } catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static void throwException(boolean condition, String msg, Class<? extends GlobalException> exceptionClass) {
        try {
            if (condition) {
                throw exceptionClass.getConstructor(String.class)
                        .newInstance(msg);

            }
        } catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static void throwException(boolean condition, String msg) {
        if (condition) {
            throw new GlobalException(msg);
        }
    }

    public static void throwException(boolean condition, Integer code, String msg) {
        if (condition) {
            throw new GlobalException(code, msg);
        }
    }

}
